package 单例模式.reflect;

import java.lang.reflect.Constructor;

/**
 * description:
 * author: wangxi
 * date:  2022/3/23 5:18 下午
 *
 * 反射破坏单例
 **/
public class SingletonByReflect {
    public static void main(String[] args) {
        try {
            Class<Singleton6> singleton6Class = Singleton6.class;
            Constructor<Singleton6> declaredConstructor = singleton6Class.getDeclaredConstructor(null);
            // 强行设置构造函数可访问
            declaredConstructor.setAccessible(true);
            // 创建两个对象，创建对象同时默认会加载整个类，自然就会创建holder类的单例对象
            Singleton6 instance01 = declaredConstructor.newInstance();
            Singleton6 instance02 = declaredConstructor.newInstance();

            System.out.println(instance01 == instance02);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
